﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace Denting
{


    public partial class Form1
    {
        //全局变量
        static public string InpFileFullPath;
        static public string OutputFileDirectory;
        static public string ImpactorFileDirectory = Directory.GetCurrentDirectory() + "\\Impactors";
        static public string ImpactorFileFullPath;
        static public string IncludeFileDirectory;

        static public List<HMComponent> ListHMComponent = new List<HMComponent>();
        static public List<HMNodeSet> ListHMNodeSet = new List<HMNodeSet>();
        static public double ImpactorRadioInTextBox=15.0;
        static public int SplitCout=2;
        static public Nodes NodeAllExceptImpactor=new Nodes();
        static public Nodes NodeOfImpactor = new Nodes();
        static public Nodes NodeOnDentingSurface = new Nodes();
        static public Elements ElementsOnDentingSurface = new Elements();
        static public int CompIDOnDentingSurface=0;
        static public string CompNameOnDentingSurface;


        public void getCompAndNodeSetInfo()
        {
            //复位操作，用于再次选择INP文件
            NodeOnDentingSurface.Clear();
            ElementsOnDentingSurface.Clear();
            ListHMComponent.Clear();
            ListHMNodeSet.Clear();
            comboBox_NodeSet.Items.Clear();
            listBox_DentingNodeID.Items.Clear();
            comboBox_NodeSet.Text = "";
            comboBox_impactor.SelectedIndex = 0;
            comboBox_SplitCount.SelectedIndex = 2;
            textSplitRadio.Enabled = false;
            comboBox_SplitCount.Enabled = false;
            textSplitRadio.Text = "15.0";
            comboBox_SplitCount.SelectedIndex = 2;
            ImpactorRadioInTextBox=15.0;
            SplitCout=2;
            radioButton1.Checked = true;
            radioButton3.Checked = true;
            textBox_nodeID.Text = "";
            textBox_CompID.Text = "";


            //////////////////////////////////////////////////////
            string tempInputFile = InpFileFullPath + ".david";
            File.Copy(InpFileFullPath, tempInputFile);
            StreamReader sr = new StreamReader(tempInputFile);
            string s = sr.ReadLine();
            ///////////////////////////////////////////////////////


            label_BigMessge.Text = "正在读取文件：\r\n";
            int compid=0;
            while (s != null)
            {
                //查找comp信息
                if (s.Length > 25 && s.Substring(0, 14) == "**HWCOLOR COMP")
                {
                    HMComponent hm_comp = new HMComponent();
                    bool IsNewComp = true;
                    compid = Convert.ToInt32(s.Substring(18,8));
                    foreach(HMComponent cp in ListHMComponent)
                    {
                        if (cp.compID==compid)
                        {
                            IsNewComp = false;
                            break;
                        }
                    }
                    if (IsNewComp)
                    {
                        hm_comp.compID = compid;
                        s = sr.ReadLine();
                        int idx=s.IndexOf("ELSET=");
                        if (idx!=-1)
                        {
                            hm_comp.compName = s.Substring(idx + 6);
                            ListHMComponent.Add(hm_comp);
                        }
                        
                        label_BigMessge.Text += compid.ToString() + " , ";
                    }
                    

                }

                //查找NODESET信息
                if (s.Length>5 &&s.Substring(0,5)=="*NSET")
                {
                    HMNodeSet ns = new HMNodeSet();
                    ns.nodeSetName = s.Substring(s.IndexOf("NSET=") + 5);
                    ns.listNode = new List<int>();
                    s = sr.ReadLine();
                    while (s.Substring(0,1)!= "*")
                    {
                        string[] sArray = s.Split(',');
                        if (sArray.Length==1)
                        {
                            ns.listNode.Add(Convert.ToInt32(s.Substring(0,s.Length-1)));
                        } 
                        else
                        {
                            foreach (string fs in sArray)
                            {
                                if (fs.Length!=0)
                                {
                                    ns.listNode.Add(Convert.ToInt32(fs));
                                }
                                
                            }
                        }
                        
                        s = sr.ReadLine();
                    }

                    ListHMNodeSet.Add(ns);
                }
                else
                {
                    //最后一行不能删
                    s = sr.ReadLine();
                }
            }
            label_BigMessge.Text += "\r\n\r\n共读取:";
            label_BigMessge.Text += ListHMComponent.Count.ToString() + "个COMP_ID 信息，请在左侧填入抗凹表面的 COMP_ID 。\r\n";

            foreach(HMNodeSet ns in ListHMNodeSet)
            {
                comboBox_NodeSet.Items.Add(ns.nodeSetName);
            }
            

            ////////////////////////////////////////////////////////
            sr.Close();
            File.Delete(tempInputFile);
            ///////////////////////////////////////////////////////
        }

        public bool IsNodeOnSurface(int nodeID)
        {
            bool b = NodeOnDentingSurface.ContainsNodeID(nodeID);
            if (!b)
            {
                label_BigMessge.Text += nodeID.ToString() + " 不在指定的COMP上，该点被忽略！\r\n\r\n";
            }
            return b;
        }

        public void SetEachStatic()
        {
            SplitCout = Convert.ToInt32(comboBox_SplitCount.Text);
            ImpactorRadioInTextBox = Convert.ToDouble(textSplitRadio.Text);
            CompIDOnDentingSurface = Convert.ToInt32(textBox_CompID.Text);
            foreach (HMComponent cp in ListHMComponent)
            {
                if (cp.compID==CompIDOnDentingSurface)
                {
                    CompNameOnDentingSurface=cp.compName;
                }
            }

            OutputFileDirectory = Path.GetDirectoryName(InpFileFullPath);
            OutputFileDirectory += "\\DentingInputFiles";
            string tempDirectory = OutputFileDirectory;
            int n = 0;
            while (Directory.Exists(OutputFileDirectory))
            {
                n++;
                OutputFileDirectory = tempDirectory + "_" +n.ToString();
            }
            IncludeFileDirectory = OutputFileDirectory + "\\includeFiles";
            Directory.CreateDirectory(OutputFileDirectory);
            Directory.CreateDirectory(IncludeFileDirectory);

            label_Help.Text = IncludeFileDirectory;


            NodeAllExceptImpactor.GetNodesFromInputFile(InpFileFullPath);
            ElementsOnDentingSurface.GetElementsFromInputFile(InpFileFullPath, CompIDOnDentingSurface);
            NodeOnDentingSurface = NodeAllExceptImpactor.FindNodesAttatchToElements(ElementsOnDentingSurface);

            if (comboBox_impactor.SelectedIndex==0)
            {
                ImpactorFileFullPath=ImpactorFileDirectory +"\\D25mm.inp";
            }
            else if (comboBox_impactor.SelectedIndex == 1)
            {
                ImpactorFileFullPath = ImpactorFileDirectory + "\\D80mm.inp";
            }
            else if (comboBox_impactor.SelectedIndex == 2)
            {
                ImpactorFileFullPath = ImpactorFileDirectory + "\\Glue_D80mm.inp";
            }
            else
            {
                ImpactorFileFullPath = ImpactorFileDirectory +"\\"+ comboBox_impactor.SelectedItem;
            }
            Debug.WriteLine("**********************************{0}", ImpactorFileFullPath);
            NodeOfImpactor.GetNodesFromInputFile(ImpactorFileFullPath);
        }

        public void WriteConstIncludeFile()
        {
            string tempInputFile = InpFileFullPath + ".david";
            File.Copy(InpFileFullPath, tempInputFile);
            StreamReader sr = new StreamReader(tempInputFile);
            StreamWriter sw = new StreamWriter(IncludeFileDirectory + "\\const.inp");

            bool canReadNode = false;
            string s = sr.ReadLine();
            while (s != null)
            {
                if (canReadNode)
                {
                    
                    if (s.Length > 0 && s[0] == '*')
                    {
                        canReadNode = false;
                        break;
                    }
                    //关键代码添加位置
                    sw.WriteLine(s);

                }
                if (s == "*BOUNDARY")
                {
                    canReadNode = true;
                }
                s = sr.ReadLine();
            }
            sr.Close();
            sw.Close();
            File.Delete(tempInputFile);
        }

        public void Write_PartA_IncludeFile()
        {
            string tempInputFile = InpFileFullPath + ".david";
            File.Copy(InpFileFullPath, tempInputFile);
            StreamReader sr = new StreamReader(tempInputFile);
            StreamWriter sw = new StreamWriter(IncludeFileDirectory + "\\partA.inp");

            string s = sr.ReadLine();
            while (s != null)
            {

                if (s.Length > 8 && s.Substring(0, 8) == "*ELEMENT" && s.Substring(s.IndexOf("ELSET=")+6)==CompNameOnDentingSurface)
                {
                    sw.WriteLine(s);
                    break;
                }
                else if (s.Length > 16 && s.Substring(0, 17) == "**HMNAME LOADSTEP")
                {
                    break;
                }
                else
                {
                    sw.WriteLine(s);
                    s = sr.ReadLine();
                }
                
                
            }

            while (s != null)
            {
                s = sr.ReadLine();
                if (s.Length>0 && s[0]=='*')
                {
                    break;
                }
            }

            while (s != null)
            {

                if (s.Length > 8 && s.Substring(0, 8) == "*ELEMENT" && s.Substring(s.IndexOf("ELSET=") + 6) == CompNameOnDentingSurface)
                {
                    sw.WriteLine(s);
                    break;
                }
                else if (s.Length > 16 && s.Substring(0, 17) == "**HMNAME LOADSTEP")
                {
                    break;
                }
                else
                {
                    sw.WriteLine(s);
                    s = sr.ReadLine();
                }
            }
            while (s != null)
            {
                s = sr.ReadLine();
                if (s.Length > 0 && s[0] == '*')
                {
                    break;
                }
            }
            while (s != null)
            {

                if (s.Length > 8 && s.Substring(0, 8) == "*ELEMENT" && s.Substring(s.IndexOf("ELSET=") + 6) == CompNameOnDentingSurface)
                {
                    sw.WriteLine(s);
                    break;
                }
                else if (s.Length > 16 && s.Substring(0, 17) == "**HMNAME LOADSTEP")
                {
                    break;
                }
                else
                {
                    sw.WriteLine(s);
                    s = sr.ReadLine();
                }
            }
            sr.Close();
            sw.Close();
            File.Delete(tempInputFile);
        }


    }




    //自定义结构
    public struct HMComponent
    {
        public int compID;
        public string compName;
    }

    public struct HMNodeSet
    {
        public string nodeSetName;
        public List<int> listNode;
    }
}
